<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="param.nickName" placeholder="请输入昵称" style="width: 200px;" class="filter-item"
                @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="reset">重置</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit"
                 @click="handleSaveData({})">添加
      </el-button>
      <el-button v-waves class="filter-item" type="danger" icon="el-icon-delete" @click="tableHandleBatchDelete">批量删除
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;"
              @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"></el-table-column>
      <el-table-column label="用户名" align="center" min-width="150px" prop="userName"></el-table-column>
      <el-table-column label="密码" align="center" min-width="150px" prop="passWord"></el-table-column>
      <el-table-column label="头像" align="center" width="130px">
        <template slot-scope="{row}">
          <el-image class="table-img" :src="row.headImg" :preview-src-list="[row.headImg]"></el-image>
          <el-upload ref="upload"
                     :action="'http://localhost:8001' +moduleUrl + '/upload/' + row.id "
                     :on-success="handleAvatarSuccess"
                     :limit="1"
                     :before-upload="beforeImageUpload"
                     :show-file-list="false">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" min-width="150px" prop="nickName"></el-table-column>
      <el-table-column label="类型" align="center" width="110px">
        <template slot-scope="{row}">
          <strong v-if="row.type == 1">管理员</strong>
          <strong v-if="row.type == 2">员工</strong>
        </template>
      </el-table-column>
      <el-table-column label="账号状态" align="center" width="110px">
        <template slot-scope="{row}">
          <strong v-if="row.status == 1">可用</strong>
          <strong v-if="row.status == 2">禁用</strong>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleSaveData(row)" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleTableDelete(row)" icon="el-icon-delete">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="param.current" :limit.sync="param.pageSize"
                @pagination="getList"/>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="form" label-position="left" label-width="150px"
               style="width: 400px; margin-left:50px;">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName"/>
        </el-form-item>
        <el-form-item label="密码" prop="passWord">
          <el-input v-model="form.passWord"/>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName"/>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio :label="1">管理员</el-radio>
            <el-radio :label="2">员工</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitTableForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import {tableUtil} from '../../utils/tableUtil';
import {batchDelete, page, saveData} from '@/api/admin/index'

export default {
  name: 'admin',
  components: {Pagination},
  directives: {waves},
  mixins: [tableUtil],
  data() {
    return {
      moduleUrl: '/admin',
      param: { // 列表查询参数
        current: 1,
        pageSize: 5,
        nickName: undefined,
      },
      form: { // 添加或修改提交表单
        id: undefined,
        userName: undefined,
        passWord: undefined,
        headImg: undefined,
        nickName: undefined,
        type: undefined,
        status: undefined,
      },
      rules: { // 自定义校验规则
        userName: [{required: true, message: '用户名为必须项~', trigger: 'blur'}],
        passWord: [{required: true, message: '密码为必须项~', trigger: 'blur'}],
        nickName: [{required: true, message: '昵称为必须项~', trigger: 'blur'}],
        type: [{required: true, message: '类型为必须项~', trigger: 'blur'}],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() { // 表格数据分页获取
      this.listLoading = true
      page(this.param).then(res => {
        this.list = res.data.records
        this.total = res.data.total
        this.listLoading = false
      })
    },
    submitTableForm() {
      this.submitForm(saveData)
    },
    handleTableDelete(row) {
      this.handleDelete(row, batchDelete)
    },
    tableHandleBatchDelete() {
      this.tableBatchDelete(batchDelete)
    },
  }
}
</script>
